package com.dingding.order.core.grab.biz.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.message.shotmsg.ShotMessageBuilder;
import com.dingding.common.core.model.push.PushModel;
import com.dingding.common.core.push.pushmsg.PushData;
import com.dingding.common.core.push.pushmsg.PushMessageBuilder;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.facade.queue.dto.msg.MessageRequestsDTO;
import com.dingding.facade.queue.dto.push.PushRequestDTO;
import com.dingding.facade.queue.enums.ClientType;
import com.dingding.facade.queue.enums.PushType;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.order.code.order.OrderStatus;
import com.dingding.order.core.grab.biz.INoticeUserService;
import com.dingding.order.core.grab.dao.INoticeUserDao;
import com.dingding.order.dto.grab.PushCompetitiveOrder;
import com.dingding.order.dto.grab.PushTaxiGrabOrder;

/**
 * @ClassName: NoticeUserServiceImpl 
 * @Description: 通知类Service实现类(推送和发送短信)
 * @author YHQ
 * @date 2016年6月4日 下午5:45:28
 */
@Component("noticeUserService")
public class NoticeUserServiceImpl implements INoticeUserService {

	@Resource(name = "noticeUserDao")
	private INoticeUserDao noticeUserDao;

	@Resource(name = "queueService")
	private IQueueFacade queueFacade;

	/**
	 * @Title: getNoticeUserInfo 
	 * @Description: 用户抢单-通知被抢用户
	 * @param userType			下单用户类型
	 * @param placeOrderUserId	下单用户
	 * @param grabOrderUserId	抢单用户
	 * @author YHQ 
	 * @return PushCompetitiveOrder    返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public void getNoticeUserInfo(Integer userType, String placeOrderUserId, String grabOrderUserId, String orderId,String parentOrderId) {

		PushCompetitiveOrder pushCompetitiveOrder = noticeUserDao.getNoticeContent(userType, placeOrderUserId,
				grabOrderUserId);

		
		pushCompetitiveOrder.setSubOrder(orderId);	//子订单Id
		pushCompetitiveOrder.setParentOrder(parentOrderId);	//主订单Id
		pushCompetitiveOrder.setGrabOrderUserId(grabOrderUserId);//抢单用户Id
		pushCompetitiveOrder.setPublishType(1);//订单类型(1:顺风车 2:出租车 3:专车4:一口价)

		// 短信
		sendMessage(pushCompetitiveOrder.getAnotherPhoneNo(),userType);

		// 推送
		pushMessage(pushCompetitiveOrder, userType);

		// 通知
		pushNoticeMessage(pushCompetitiveOrder, userType);
	}

	/**
	 * 
	 * @Title: senMessage
	 * @Description: 发送短信 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	public void sendMessage(String tellNo,Integer userType) {

		MessageRequestsDTO messageRequestsDTO = new MessageRequestsDTO();
		// 手机号
		messageRequestsDTO.setTelPhone(tellNo);
		
		if(userType == 1){
			// 短信内容
			messageRequestsDTO.setMessageContent(ShotMessageBuilder.PASSENGER_GRAB_ORDER);
		}else{
			// 短信内容
			messageRequestsDTO.setMessageContent(ShotMessageBuilder.DRIVER_GRAB_ORDER);
		}
		// 发送短信加入队列
		queueFacade.addMessageToQueue(JSONUtil.toJSONString(messageRequestsDTO));

	}
	
	/**
	 * 
	 * @Title: sendTaxiMessage
	 * @Description: 发送短信 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	public void sendTaxiMessage(String tellNo,Integer userType) {
		
		MessageRequestsDTO messageRequestsDTO = new MessageRequestsDTO();
		// 手机号
		messageRequestsDTO.setTelPhone(tellNo);
		
		messageRequestsDTO.setMessageContent(ShotMessageBuilder.TAXI_DRIVER_GRAB_ORDER);
		
		// 发送短信加入队列
		queueFacade.addMessageToQueue(JSONUtil.toJSONString(messageRequestsDTO));
		
	}

	/**
	 * 
	 * @Title: pushMessage
	 * @Description: 用户抢单-推送消息 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	@Override
	public void pushMessage(PushCompetitiveOrder pushCompetitiveOrder, Integer userType) {

		// 用户抢单-推送消息DTO对象
		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(pushCompetitiveOrder.getAliasId());

		// 设备类型
		if (pushCompetitiveOrder.getLoginEquipment().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (pushCompetitiveOrder.getLoginEquipment().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		pushCompetitiveOrder.setGrabOrderTime(new Date());
		pushCompetitiveOrder.setOrderStatus(OrderStatus.ORDER_STATUS_PAY);

		// 推送title和alert
		if (userType == 2) { // 当前用户类型为乘客
			pushRequestDTO.setAlert(PushMessageBuilder.ORDER_ALREADY_ROBBED_PASSENGER);
			pushRequestDTO.setMsgTitle(PushMessageBuilder.ORDER_ALREADY_ROBBED_PASSENGER);
		} else if (userType == 1) { // 当前用户类型为司机
			pushRequestDTO.setAlert(PushMessageBuilder.ORDER_ALREADY_ROBBED_DRIVER);
			pushRequestDTO.setMsgTitle(PushMessageBuilder.ORDER_ALREADY_ROBBED_DRIVER);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.alert_msgcontent);

		// 创建推送Bean
		PushData pushData = new PushData();

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("competitiveOrderPush");
		// 推送内容
		pushData.setResponseBody(pushCompetitiveOrder);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData,"yyyy-MM-dd HH:mm:ss"));

		// 推送消息加入队列
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));

	}
	
	/**
	 * 
	 * @Title: pushMessage
	 * @Description: 用户抢单-推送消息 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	@Override
	public void pushNoticeMessage(PushCompetitiveOrder pushCompetitiveOrder, Integer userType) {
		
		// 用户抢单-推送消息DTO对象
		PushRequestDTO pushRequestDTO = new PushRequestDTO();
		
		// 推送别名
		pushRequestDTO.setAliasId(pushCompetitiveOrder.getAliasId());
		
		// 设备类型
		if (pushCompetitiveOrder.getLoginEquipment().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (pushCompetitiveOrder.getLoginEquipment().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}
		
		pushCompetitiveOrder.setGrabOrderTime(new Date());
		pushCompetitiveOrder.setOrderStatus(OrderStatus.ORDER_STATUS_PAY);
		
		String titleStr = null;
		
		if(userType == 1){ // 司机下单
			titleStr = ShotMessageBuilder.PASSENGER_GRAB_ORDER;
		}else{	// 乘客下单
			titleStr = ShotMessageBuilder.DRIVER_GRAB_ORDER;
		}
		
		// 消息推送类型
		pushRequestDTO.setPushType(PushType.msgcontent);
		
		// 创建推送Bean
		PushData pushData = new PushData();
		
		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("newsNoticePush");
		// 推送内容
		pushData.setResponseBody(PushModel.noticeModel(1, titleStr, null));
		
		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData,"yyyy-MM-dd HH:mm:ss"));
		
		// 推送消息加入队列
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
		
	}

	/**
	 * 
	 * @Title: pushMessage
	 * @Description: 用户抢单-推送消息 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	@Override
	public void pushTaxiMessage(PushTaxiGrabOrder pushTaxiGrabOrder, Integer userType) {

		// 用户抢单-推送消息DTO对象
		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(pushTaxiGrabOrder.getAliasId());

		// 设备类型
		if ("Android".equals(pushTaxiGrabOrder.getLoginEquipment())) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (pushTaxiGrabOrder.getLoginEquipment().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		pushTaxiGrabOrder.setGrabOrderTime(new Date());
		pushTaxiGrabOrder.setOrderStatus(OrderStatus.ORDER_STATUS_PAY);

		// 推送title和alert
		if (userType == 2) { // 当前用户类型为乘客
			pushRequestDTO.setAlert(PushMessageBuilder.ORDER_ALREADY_ROBBED_DRIVER);
			pushRequestDTO.setMsgTitle(PushMessageBuilder.ORDER_ALREADY_ROBBED_DRIVER);
		} else if (userType == 1) { // 当前用户类型为司机
			pushRequestDTO.setAlert(PushMessageBuilder.ORDER_ALREADY_ROBBED_PASSENGER);
			pushRequestDTO.setMsgTitle(PushMessageBuilder.ORDER_ALREADY_ROBBED_PASSENGER);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.alert_msgcontent);

		// 创建推送Bean
		PushData pushData = new PushData();

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("competitiveOrderPush");
		// 推送内容
		pushData.setResponseBody(pushTaxiGrabOrder);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData,"yyyy-MM-dd HH:mm:ss"));

		// 推送消息加入队列
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));

	}

	
	/**
	 * 
	 * @Title: pushTaxiNoticeMessage
	 * @Description: 用户抢单-推送通知消息 
	 * @author yuHaiQing
	 * @return void    返回类型
	 * 
	 */
	@Override
	public void pushTaxiNoticeMessage(PushTaxiGrabOrder pushTaxiGrabOrder, Integer userType) {
		
		// 用户抢单-推送消息DTO对象
		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(pushTaxiGrabOrder.getAliasId());

		// 设备类型
		if ("Android".equals(pushTaxiGrabOrder.getLoginEquipment())) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (pushTaxiGrabOrder.getLoginEquipment().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.msgcontent);

		// 创建推送Bean
		PushData pushData = new PushData();

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("newsNoticePush");
		// 推送内容
		pushData.setResponseBody(PushModel.noticeModel(1, ShotMessageBuilder.TAXI_DRIVER_GRAB_ORDER, null));

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData,"yyyy-MM-dd HH:mm:ss"));

		// 推送消息加入队列
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
		
	}
	
	
}
